package com.xros.demo.fileserver.utils;

import lombok.extern.log4j.Log4j2;

import java.io.*;

/**
 * @Author: mzp
 * @Description: 自定义字符串工具类
 * @Date: 2024/1/30 17:25
 * @Version: 1.0
 */
@Log4j2
public final class SelfStringUtils {
    public static String getExceptionStr(Exception e) {
        //读取异常栈信息
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(arrayOutputStream));
        //通过ByteArray转换输入输出流
        BufferedReader fr = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arrayOutputStream.toByteArray())));
        String str;
        StringBuilder exceptionStr = new StringBuilder();


        try {
            while ((str = fr.readLine()) != null) {
                exceptionStr.append(str + "\n");
            }
            //关闭流
            fr.close();
            return exceptionStr.toString();
        } catch (Exception ee) {

            log.error("类：{} 执行方法：{}时异常！异常类型：{}，异常的消消息如下：\n{}", SelfStringUtils.class.getName(), "getExceptionStr", e.getClass().getName(), e.getMessage());
        }
        return "";
    }

}
